home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / Generic List Library / sample.c < prev   
Encoding:
C/C++ Source or Header  |  1994-11-09  |  2.4 KB  |  107 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include "generic_list.h"
  3.  
  4. #define EXIT_FAILURE 1
  5.  
  6. typedef struct {
  7.     char letter;
  8.     int x;
  9.     int y;
  10. } Bob;
  11.  
  12. Bob *new_bob(char letter, int x, int y);
  13. int is_even(const Bob *bob, const void *args);
  14. void print_bob(Bob *bob, void *args);
  15. void make_odd(Bob *bob, void *args);
  16.  
  17. main()
  18. {
  19.     Generic_queue bobqueue;
  20.     Generic_list even_ones;
  21.     Bob *bob;
  22.     char c;
  23.     int i, j;
  24.  
  25.     initialize_queue(&bobqueue);
  26.  
  27.     for (c='a', i=1, j=1; i<9; c++, i++, j+=2) {
  28.         printf("Creating and queueing Bob %c) %d, %d.\n", c, i, j);
  29.         enqueue(bobqueue, new_bob(c, i, j));
  30.     }
  31.  
  32.     even_ones = all_such_that(bobqueue,
  33.                               (int(*)(const void*,const void*))is_even, NULL);
  34.  
  35.     printf("\nThese are the even Bobs:\n\n");
  36.     perform_on_list(even_ones, (void(*)(void*,void*))print_bob, NULL);
  37.  
  38.     perform_on_list(even_ones, (void(*)(void*,void*))make_odd, NULL);
  39.  
  40.     printf("\nNow they are odd:\n\n");
  41.     perform_on_list(even_ones, (void(*)(void*,void*))print_bob, NULL);
  42.  
  43.     printf("\nThe last Bob in this list is:\n\n");
  44.     print_bob(peek_at_end(even_ones), NULL);
  45.  
  46.     destroy_list(&even_ones);
  47.  
  48.     printf("\nNow let's empty our original queue.\n");
  49.     printf("The appropriate Bobs have been changed here as well.\n\n");
  50.  
  51.     while (bob = dequeue(bobqueue)) {
  52.         print_bob(bob, NULL);
  53.         free(bob);
  54.     }
  55.  
  56.     destroy_queue(&bobqueue);
  57. }
  58.  
  59. /****************************************************************************/
  60.  
  61. Bob *
  62. new_bob(char letter, int x, int y)
  63. {
  64.     Bob *bob;
  65.  
  66.     bob = (Bob *) malloc(sizeof(Bob));
  67.     if (!bob) {
  68.         fprintf(stderr, "Error allocating memory for a Bob.\n");
  69.         exit(EXIT_FAILURE);
  70.     }
  71.  
  72.     bob->letter = letter;
  73.     bob->x = x;
  74.     bob->y = y;
  75.  
  76.     return bob;
  77. }
  78.  
  79. /****************************************************************************/
  80.  
  81. int
  82. is_even(const Bob *bob, const void *args)
  83. {
  84.     if ((bob->x + bob->y)%2 == 0)
  85.         return 1;  /* TRUE, this Bob is even. */
  86.     else
  87.         return 0;  /* FALSE, this Bob is odd. */
  88. }
  89.  
  90. /****************************************************************************/
  91.  
  92. void
  93. print_bob(Bob *bob, void *args)
  94. {
  95.     printf("%c) %d + %d = %d\n", bob->letter, bob->x, bob->y, bob->x + bob->y);
  96. }
  97.  
  98. /****************************************************************************/
  99.  
  100. void
  101. make_odd(Bob *bob, void *args)
  102. {
  103.     bob->y++;
  104. }
  105.  
  106. /****************************************************************************/
  107.